package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.ModelHis;
import com.moody.hibernate.domain.ReportList;

/**
 * A data access object (DAO) providing persistence and search support for
 * ModelHis entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.ModelHis
 * @author MyEclipse Persistence Tools
 */

public class ModelHisDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(ModelHisDAO.class);
	// property constants
	public static final String MODEL_NAME = "modelName";
	public static final String MODEL_VERSION = "modelVersion";
	public static final String MODEL_KIND1 = "modelKind1";
	public static final String MODEL_KIND2 = "modelKind2";
	public static final String MODEL_STATE = "modelState";
	public static final String DELETE_FLAG = "deleteFlag";
	public static final String START_DATE = "startDate";
	public static final String END_DATE = "endDate";
	public static final String UPDATE_LOG = "updateLog";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(ModelHis transientInstance) {
		log.debug("saving ModelHis instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ModelHis persistentInstance) {
		log.debug("deleting ModelHis instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ModelHis findById(java.lang.String id) {
		log.debug("getting ModelHis instance with id: " + id);
		try {
			ModelHis instance = (ModelHis) getHibernateTemplate().get(
					"com.moody.hibernate.domain.ModelHis", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ModelHis instance) {
		log.debug("finding ModelHis instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ModelHis instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ModelHis as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByModelName(Object modelName) {
		return findByProperty(MODEL_NAME, modelName);
	}

	public List findByModelVersion(Object modelVersion) {
		return findByProperty(MODEL_VERSION, modelVersion);
	}

	public List findByModelKind1(Object modelKind1) {
		return findByProperty(MODEL_KIND1, modelKind1);
	}

	public List findByModelKind2(Object modelKind2) {
		return findByProperty(MODEL_KIND2, modelKind2);
	}

	public List findByModelState(Object modelState) {
		return findByProperty(MODEL_STATE, modelState);
	}

	public List findByDeleteFlag(Object deleteFlag) {
		return findByProperty(DELETE_FLAG, deleteFlag);
	}

	public List findByStartDate(Object startDate) {
		return findByProperty(START_DATE, startDate);
	}

	public List findByEndDate(Object endDate) {
		return findByProperty(END_DATE, endDate);
	}

	public List findByUpdateLog(Object updateLog) {
		return findByProperty(UPDATE_LOG, updateLog);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all ModelHis instances");
		try {
			String queryString = "from ModelHis";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ModelHis merge(ModelHis detachedInstance) {
		log.debug("merging ModelHis instance");
		try {
			ModelHis result = (ModelHis) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ModelHis instance) {
		log.debug("attaching dirty ModelHis instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ModelHis instance) {
		log.debug("attaching clean ModelHis instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ModelHisDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ModelHisDAO) ctx.getBean("ModelHisDAO");
	}

	//未发布的按时间排序
	@SuppressWarnings("unchecked")
	public List<ModelHis> findNotReleasedModel() {
		String queryString = "from ModelHis as model where model.modelState = ? and model.deleteFlag <> ? order by model.updateDate desc";
		return getHibernateTemplate().find(queryString,ModelHis.NOT_RELEASED,ModelHis.DELETED);
	}
	
	//其他状态(非未发布)按时间排序
	@SuppressWarnings("unchecked")
	public List<ModelHis> findNonNotReleasedModel(){
		String queryString = "from ModelHis as model where model.modelState <> ? and model.deleteFlag <> ? order by model.updateDate desc";
		return getHibernateTemplate().find(queryString,ModelHis.NOT_RELEASED,ModelHis.DELETED);
	}

	public void update(ModelHis transientInstance) {
		log.debug("updating ModelHis instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public ModelHis findByKind1AndKind2AndState(String kind1, String kind2, String state) {
		String queryString = "from ModelHis as model where model.modelKind1 = ? and model.modelKind2 = ? and model.modelState = ?";
		List<ModelHis> list = getHibernateTemplate().find(queryString, kind1, kind2, state);
		
		if(list!=null && list.size()>0)
			return list.get(list.size()-1);
		else
			return null;		
	}

	public boolean findModelByKind1AndKind2AndVersion(String kind1,
			String kind2, String version) {
		String queryString = "from ModelHis as model where model.modelKind1 = ? and model.modelKind2 = ? and model.modelVersion = ?";
		List<ModelHis> list = getHibernateTemplate().find(queryString, kind1, kind2, version);
		
		if(list!=null && list.size()>0)
			return true;
		else
			return false;	
	}

	public List<ModelHis> findEffectedModelHis() {
		String queryString = "from ModelHis as model where model.modelState = ?";
		List<ModelHis> list = getHibernateTemplate().find(queryString, ModelHis.EFFECTED);
		
		return list;
	}

}